<template>
  <div class="components-container">
    <!-- 新增 修改-->
    <sweet-modal ref="dialog" class="dialog">
      <h3>{{ title }}</h3>
      <div style=" width:500px; margin:30px 50px">
        <div class="layui-form-item">
          <label class="layui-form-label">车牌号</label>
          <div class="layui-input-block">
            <el-input type="text" v-model="carData.CARLIC"></el-input>
          </div>
        </div>
        <div class="layui-form-item">
          <label class="layui-form-label">驾驶员</label>
          <div class="layui-input-block">
            <el-select v-model="carData.CARDRIVER"  placeholder="请选择驾驶员" style="width:100%">
              <el-option label="不选择" :value="''"></el-option>
              <el-option v-for="item in drivers" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
          </div>
        </div>
        <div class="layui-form-item">
          <label class="layui-form-label">车辆类别</label>
          <div class="layui-input-block">
            <el-select v-model="carData.CARCODNAME" placeholder="请选择车辆类别" style="width:100%">
              <el-option v-for="item in carTypes" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
          </div>
        </div>
        <div class="layui-form-item">
          <label class="layui-form-label">排序</label>
          <div class="layui-input-block">
            <el-input type="text" v-model="carData.CARNUM"  oninput ="value=value.replace(/[^\d]/g,'')"></el-input>
          </div>
        </div>
        <div class="layui-form-item">
          <label class="layui-form-label">使用日期</label>
          <div class="layui-input-block">
            <el-date-picker v-date v-model="carData.CARSTARTDATE" type="date" placeholder="选择日期" style="width: 100%;">
            </el-date-picker>
          </div>
        </div>
        <div class="layui-form-item">
          <label class="layui-form-label">识别码</label>
          <div class="layui-input-block">
            <el-input type="text" v-model="carData.CARIDENTIFICATIONCODE"></el-input>
          </div>
        </div>
        <div class="layui-form-item">
          <label class="layui-form-label">品牌</label>
          <div class="layui-input-block">
            <el-input type="text" v-model="carData.CARBRAND"></el-input>
          </div>
        </div>
      </div>
      <div class="layui-form-item btnCenter">
          <el-button @click="closeDialog">取消</el-button>
          <el-button type="primary"  @click="submit" :loading="this.$store.state.isloading">提交</el-button>
      </div>
    </sweet-modal>
    <!-- 详情 -->
    <sweet-modal ref="dialogInfo" class="dialog">
      <h3>{{ title }}</h3>
      <div style=" width:500px; margin:30px 50px">
        <div class="layui-form-item">
          <label class="layui-form-label">车牌号</label>
          <div class="layui-input-block">
            <el-input type="text" v-model="carData.CARLIC" readonly></el-input>
          </div>
        </div>
        <div class="layui-form-item">
          <label class="layui-form-label">驾驶员</label>
          <div class="layui-input-block">
            <el-input type="text" v-model="carData.STANAME" readonly></el-input>
          </div>
        </div>
        <div class="layui-form-item">
          <label class="layui-form-label">车辆类别</label>
          <div class="layui-input-block">
            <el-input type="text" v-model="carData.CARCODNAMEDESC" readonly></el-input>
          </div>
        </div>
        <div class="layui-form-item">
          <label class="layui-form-label">排序</label>
          <div class="layui-input-block">
            <el-input type="text" v-model="carData.CARNUM" readonly></el-input>
          </div>
        </div>
        <div class="layui-form-item">
          <label class="layui-form-label">使用日期</label>
          <div class="layui-input-block">
            <el-input type="text" v-model="carData.CARSTARTDATE" readonly></el-input>
          </div>
        </div>
        <div class="layui-form-item">
          <label class="layui-form-label">识别码</label>
          <div class="layui-input-block">
            <el-input type="text" v-model="carData.CARIDENTIFICATIONCODE" readonly></el-input>
          </div>
        </div>
        <div class="layui-form-item">
          <label class="layui-form-label">品牌</label>
          <div class="layui-input-block">
            <el-input type="text" v-model="carData.CARBRAND" readonly></el-input>
          </div>
        </div>
      </div>
      <div class="layui-form-item btnCenter">
          <el-button @click="closeDialog">取消</el-button>
      </div>
    </sweet-modal>
  </div>
</template>
<style rel="stylesheet/scss" lang="scss" scoped>
@import "src/styles/common.scss";

</style>
<script>
import apiBase from "@/api/apiBase.js";
import {
  formatDate,
  timeStringToDate,
  dateStringToDate
} from "@/utils/date.js";
import sweetModal from "@/components/sweetModal";

const ADD = "新增";
const MODIFY = "修改";
const DELETE = "删除";
const INFO = "详情";

export default {
  name: "carsManageMain",
  props: {
    orderType: {
      type: String,
      default() {
        return ADD;
      }
    }
  },
  data() {
    return {
      title: null,
      carTypes: [],
     
     
      carData: { },
      drivers:[]
    };
  },
  methods: {
    //获取司机
    getDrivers() {
      let data = {};
      apiBase.DoService(data, apiBase.getApiAdress().Staff.QueryDriver, res => {
        let driverData = res.data.content,
          legendsDrivers = driverData.map(item => ({
            label: item.STANAME,
            value: item.STAID
          }));
        this.drivers = legendsDrivers;
    
      });
    },
    //获取车辆类型
    getCarType() {
      let data = {};
      apiBase.DoService(data, apiBase.getApiAdress().Code.GetCarType, res => {
        let carTypeData = res.data.content,
          legendsUsersData = carTypeData.map(item => ({
            label: item.CodeName,
            value: item.CodeValue
          }));
        this.carTypes = legendsUsersData;
      });
    },
    //打开页面
    open(order, id) {
      this.order = order;

      //清理数据
      this.dataRepeat();
      switch (this.order) {
        case ADD:
          this.title = "车辆新增";
          this.initSelectData();
          this.$refs.dialog.open();
          break;
        case MODIFY:
          this.$refs.dialog.open();
          this.initSelectData();
          this.title = "车辆修改";
          this.dataInfo(id);
          break;
        case DELETE:
          this.dataDelete(id);
          break;
        case INFO:
          this.$refs.dialogInfo.open();
          this.title = "车辆详情";
          this.dataInfo(id);
          break;
        default:
      }
    },
    //提交
    submit() {
      this.$store.state.isloading = true;
      switch (this.order) {
        case ADD:
          this.dataAdd();
          break;
        case MODIFY:
          this.dataModify();
          break;
        case DELETE:
          break;
        case INFO:
          this.$refs.dialog.close();
          break;
        default:
      }
    },
    //车辆新增
    dataAdd() {
      
      this.carData.CARSTARTDATE = formatDate(this.carData.CARSTARTDATE, "yyyyMMdd");
      let Data = JSON.parse(JSON.stringify(this.carData));
     
     
      apiBase.DoService(
        Data,
        apiBase.getApiAdress().Car.Create,
        res => {
          this.$refs.dialog.close();
        //  console.log('carData',this.carData)
          this.$message({
            type: "success",
            message: "新增成功!"
          });
         
          this.refreshData();
        },
        res => {
          this.$message.error(res.data.msg);
         
        }
      );
    },
    //车辆修改
    dataModify() {
     
      let Data = JSON.parse(JSON.stringify(this.carData));
      Data.CARSTARTDATE = formatDate(Data.CARSTARTDATE, "yyyyMMdd");
     
      apiBase.DoService(
        Data,
        apiBase.getApiAdress().Car.Modify,
        res => {
          this.$refs.dialog.close();
          this.$message({
            type: "success",
            message: "修改成功!"
          });
          
          this.refreshData();
        },
        res => {
          this.$message.error(res.data.msg);
         
        }
      );
    },
    //车辆删除
    dataDelete(id) {
      let Data = { CARID: id };
      apiBase.DoService(
        Data,
        apiBase.getApiAdress().Car.Delete,
        res => {
          this.$message({
            type: "success",
            message: "删除成功!"
          });
          this.refreshData();
        },
        res => {
          this.$message.error(res.data.msg);
          
        }
      );
    },
    //车辆详情
    dataInfo(id) {
      let Data = { CARID: id };
      apiBase.DoService(Data, apiBase.getApiAdress().Car.Detail, res => {
        this.carData = res.data.content;
        this.carData.CARID = id;
      });
    },
    //数据重置
    dataRepeat() {
      this.carData = {};
    },
    //初始化下拉框数据
    initSelectData() {
      this.getCarType();
      this.getDrivers();
    },
    refreshData() {
      this.$emit("refreshData");
    },
    closeDialog() {
      if (this.order === ADD || this.order === MODIFY)
        this.$refs.dialog.close();
      else if (this.order === INFO) this.$refs.dialogInfo.close();
    }
  },
  components: {
    sweetModal
  },
  mounted() {},
  created() {},
  // watch: {
  //   "carData.CARSTARTDATE": function(val) {
  //     this.carData.CARSTARTDATE = formatDate(val, "yyyy-MM-dd");
  //   }
  // }
};
</script>
